package com.hos.check.common.model;

import java.io.Serializable;
import java.lang.reflect.Method;

import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import com.hos.check.common.util.IDGenerator;
import com.hos.check.common.util.ModelUtil;

public class BaseModel implements IBaseModel, Serializable {

	private String fdId;
	
	public String getFdId() {
		if (fdId == null) {
			fdId = IDGenerator.generateID();
		}
		return fdId;
	}


	public void setFdId(String id) {
		// TODO Auto-generated method stub
		this.fdId=id;
	}
	
	/**
	 * 覆盖toString方法，使用列出域模型中的所有get方法返回的值（不获取返回值类型为非java.lang.*的值）
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		try {
			Method[] methodList = this.getClass().getMethods();
			ToStringBuilder rtnVal = new ToStringBuilder(this,
					ToStringStyle.MULTI_LINE_STYLE);
			for (int i = 0; i < methodList.length; i++) {
				String methodName = methodList[i].getName();
				if (methodList[i].getParameterTypes().length > 0
						|| !methodName.startsWith("get")
						|| methodName.equals("getClass"))
					continue;
				methodName = methodList[i].getReturnType().toString();
				if ((methodName.startsWith("class") || methodName
						.startsWith("interface"))
						&& !(methodName.startsWith("class java.lang.") || methodName
								.startsWith("interface java.lang.")))
					continue;
				try {
					rtnVal.append(methodList[i].getName().substring(3),
							methodList[i].invoke(this, null));
				} catch (Exception e) {
				}
			}
			return rtnVal.toString().replaceAll("@[^\\[]+\\[\\r\\n", "[\r\n");
		} catch (Exception e) {
			return super.toString();
		}
	}

	/**
	 * 覆盖equals方法，仅比较类型是否相等以及关键字是否相等
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object object) {
		if (this == object)
			return true;
		if (object == null)
			return false;
		if (!ModelUtil.getModelClassName(object).equals(
				ModelUtil.getModelClassName(this)))
			return false;
		BaseModel objModel = (BaseModel) object;
		return objModel.getFdId().equals(this.getFdId());
	}

	/**
	 * 覆盖hashCode方法，通过模型中类名和ID构建哈希值
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		HashCodeBuilder rtnVal = new HashCodeBuilder(-426830461, 631494429);
		rtnVal.append(ModelUtil.getModelClassName(this));
		rtnVal.append(getFdId());
		return rtnVal.toHashCode();
	}

}
